#include "muduo/proto/codec.h"
#include "muduo/proto/dispatcher.h"
#include "muduo/base/Logging.h"
#include "muduo/base/Mutex.h"
#include "muduo/net/EventLoop.h"
#include "muduo/net/TcpClient.h"
#include "muduo/net/EventLoopThread.h"
#include "muduo/base/CountDownLatch.h"

#include "request.pb.h"

#include <iostream>
#include <unordered_map>

#define _1 std::placeholders::_1
#define _2 std::placeholders::_2
#define _3 std::placeholders::_3

class Client
{
public:
    typedef std::shared_ptr<myrequest::TranslateRequest> TranslateRequestPtr;
    typedef std::shared_ptr<myrequest::AddResquest> AddResquestPtr;
    typedef std::shared_ptr<myrequest::TranslateRespone> TranslateResponsePtr;
    typedef std::shared_ptr<myrequest::AddRespone> AddResponsePtr;
public:
    Client(const std::string& sip, const uint16_t port)
    : _client(_loopthread.startLoop(), muduo::net::InetAddress(sip, port), "Client")
    , _latch(1)
    , _dispatcher(std::bind(&Client::onUnknownMessage, this, _1, _2, _3))
    , _codec(std::bind(&ProtobufDispatcher::onProtobufMessage, &_dispatcher, _1, _2, _3))
    {
        _dispatcher.registerMessageCallback<myrequest::TranslateRespone>(
            std::bind(&Client::onTranslate, this, _1, _2, _3));
        _dispatcher.registerMessageCallback<myrequest::AddRespone>(
            std::bind(&Client::onAdd, this, _1, _2, _3));
        _client.setConnectionCallback(
            std::bind(&Client::onConnect, this, _1));
        _client.setMessageCallback(
            std::bind(&ProtobufCodec::onMessage, &_codec, _1, _2, _3));
    }
    void Connect()
    {
        _client.connect();
        _latch.wait();
    }
    void Add(const int32_t op1, const int32_t op2)
    {
        myrequest::AddResquest req;
        req.set_op1(op1);
        req.set_op2(op2);
        send(&req);
    }
    void Translate(const std::string& msg)
    {
        myrequest::TranslateRequest req;
        req.set_msg(msg);
        send(&req);
    }
private:
    bool send(const google::protobuf::Message* msg)
    {
        if (_conn->connected() == true)
        {
            _codec.send(_conn, *msg);
            return true;
        }
        return false;
    }
    void onTranslate(const muduo::net::TcpConnectionPtr& conn,
        const TranslateResponsePtr& message,
        muduo::Timestamp)
    {
        std::cout << "翻译结果：" << message->msg() << std::endl;
    }
    void onAdd(const muduo::net::TcpConnectionPtr& conn,
               const AddResponsePtr& message,
               muduo::Timestamp)
    {
        std::cout << "加法请求：" << message->result() << std::endl;
    }
    void onConnect(const muduo::net::TcpConnectionPtr& conn)
    {
        if (conn->connected() == true)
        {
            _conn = conn;
            _latch.countDown();
        }
        else
        {
            // 连接断开的情况
            _conn.reset();
        }
    }
    void onUnknownMessage(const muduo::net::TcpConnectionPtr&,
                        const MessagePtr& message,
                        muduo::Timestamp)
    {
        LOG_INFO << "onUnknownMessage: " << message->GetTypeName();
    }
private:
    muduo::net::EventLoopThread _loopthread;
    muduo::net::TcpClient _client;
    muduo::net::TcpConnectionPtr _conn;
    muduo::CountDownLatch _latch;
    ProtobufDispatcher _dispatcher;
    ProtobufCodec _codec;
};

int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " server-ip server-port" << std::endl;
        exit(0);
    }
    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);
    Client client(serverip, serverport);
    client.Connect();
    client.Translate("hello");
    client.Add(11, 12);
    sleep(1);

    return 0;
}